/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: SysLoginDao.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年1月22日
 */
package com.sirdc.modules.sys.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysLoginFilter;
import com.sirdc.modules.sys.filter.SysLoginLogFilter;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.utils.CollectionsUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.TaskUnit;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


/**
 * @version Revision: 0.0.1
 */
@Repository
public class SysLoginDao extends StringDao<SysLogin> {


    /**
     * 查看全部的用户登陆信息
     *
     * @param filter
     * @return
     */
    public List<SysLogin> query(SysLoginFilter filter) {
        GenericQuery query = createQuery("obj");
        return query.listResult(filter);
    }

    /**
     * @param token
     * @return
     */
    public SysLogin getUserByLoginName(String token) {
        String hql = "from SysLogin login where login.username=:username and state=:state";
        GenericQuery query = create(hql);
        query.setParam("username", token);
        query.setParam("state", "启用");
        return query.singleResult();
    }

    /**
     * @param mail
     * @return
     */
    public SysLogin getLoginByEmail(String mail) {
        String hql = "from SysLogin login where login.email=:email";
        GenericQuery query = create(hql);
        query.setParam("email", mail);
        return query.singleResult();
    }

    /**
     * @param mobile
     * @return
     */
    public SysLogin getLoginByMobile(String mobile) {
        String hql = "from SysLogin login where login.mobile=:mobile";
        GenericQuery query = create(hql);
        query.setParam("mobile", mobile);
        return query.singleResult();
    }

    /**
     * @param userId
     * @return
     */
    public SysLogin getLoginByUserId(String userId) {
        String hql = "from SysLogin login where login.sysId=:userId";
        GenericQuery query = create(hql);
        query.setParam("userId", userId);
        return query.singleResult();
    }

    /**
     * @param filter
     * @return
     */
    public List<String> queryLoginByRoleDept(SysUserFilter filter) {
        GenericQuery query = create("select sysId from SysLogin where roleId=:roleId and deptId=:deptId");
        query.setParam("roleId", filter.getRoleId());
        query.setParam("deptId", filter.getDeptId());
        return query.listResult();
    }

    /**
     * @param userId
     * @param roleId
     * @param deptId
     * @return
     */
    public boolean isExistUserPermission(String userId, String roleId, String deptId) {
        GenericQuery query = create("from SysLogin where roleId=:roleId and deptId=:deptId and sysId=:userId");
        query.setParam("roleId", roleId);
        query.setParam("deptId", deptId);
        query.setParam("userId", userId);
        return (query.count() > 0);
    }

    /**
     * @param sno
     * @return
     * @author: huiyang.yu
     */
    public SysLogin getLoginBySno(String sno) {
        String hql = "from SysLogin login where login.sno=:sno";
        GenericQuery query = create(hql);
        query.setParam("sno", sno);
        return query.singleResult();
    }

    public SysLogin getLoginByUsername(String username) {
        String hql = "from SysLogin login where login.username=:username  and state =:state ";
        GenericQuery query = create(hql);
        query.setParam("username", username);
        query.setParam("state", "启用");
        return query.singleResult();
    }

    public SysLogin getLoginByUsernameDept(String username, String deptId) {
        String hql = "from SysLogin login where login.username=:username and login.deptId=:deptId";
        GenericQuery query = create(hql);
        query.setParam("username", username);
        query.setParam("deptId", deptId);
        return query.singleResult();
    }

    public List<SysLogin> queryUserLoginByRoleDept(SysUserFilter filter) {
        GenericQuery query = createQuery("obj");
        if (StringUtils.isNotBlank(filter.getRoleId())) {
            query.append(" and obj.roleId =:roleId").setParam("roleId", filter.getRoleId());
        }
        if (StringUtils.isNotBlank(filter.getDeptId())) {
            query.append(" and obj.deptId =:deptId").setParam("deptId", filter.getDeptId());
        }
        query.setOrder("userSort", BaseFilter.ASC);
        List<SysLogin> sysLogins = query.listResult();

        //过滤sysUser表中state不为01的数据
        StringBuilder sql = new StringBuilder();
        sql.append("select t1.sys_id as sysId, t1.name as name,t1.mobile as mobile, t1.position as position,t1.state as state from t_sys_user t1 where 1=1");
        sql.append(" and t1.state = '01'");
        SQLQuery sqlQuery = getSession().createSQLQuery(sql.toString());
        sqlQuery.setResultTransformer(Transformers.aliasToBean(SysUser.class));
        List<SysUser> userList = sqlQuery.list();

        List<SysLogin> result = new ArrayList<>();
        for (SysLogin sysLogin : sysLogins) {
            for (SysUser sysUser : userList) {
                if(sysLogin.getSysId().equals(sysUser.getSysId())){
                    if("01".equals(sysUser.getState())){
                        result.add(sysLogin);
                        break;
                    }
                }
            }
        }
        return result;
    }


    /**
     * 查询该角色的上级人员
     *
     * @param
     * @return
     */
    public List<SysLogin> queryLeaderList(String deptId, String role) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.roleId <:roleId").setParam("roleId", role);
        query.append(" and obj.roleId >:role").setParam("role", "1");
        query.append(" and obj.deptId =:deptId").setParam("deptId", deptId);
        return query.listResult();
    }

    /**
     * 根据用户姓名和角色查询login信息
     *
     * @param name
     * @param roleId
     * @return
     */
    public List<SysLogin> getByNameAndRoleId(String name, String roleId) {
        StringBuilder sql = new StringBuilder();
        sql.append("select t1.sys_id sysId,t1.dept_sys_id deptId,t1.username as username from t_sys_login t1, t_sys_user t2 where t1.sys_id = t2.user_id and t2.name = '" + name + "' and t1.role_id = '" + roleId + "'");
        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(SysLogin.class));
        List<SysLogin> list = query.list();
        return list;
    }

    public List<SysLogin> getByUsernameDept(String s, String deptId) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.username =:username").setParam("username", s);
        query.append(" and obj.deptId =:deptId").setParam("deptId", deptId);
        return query.listResult();
    }

    public void batchDeleteLogin(List<String> ids) {
        if (CollectionsUtils.isNotEmpty(ids)) {
            GenericQuery query = create("update SysLogin login set login.state=:state").setParam("state", "禁用");
            query.append(" where login.id in (:ids)").setParamList("ids", ids);
            query.executeUpdate();
        }
    }

    public List<SysLogin> queryList() {
        String hql = "from SysLogin login where login.state=:state";
        GenericQuery query = create(hql);
        query.setParam("state", "启用");
        return query.listResult();
    }

    public List<SysLogin> queryLog(SysLoginLogFilter filter) {
        GenericQuery query = createQuery("obj");
        if (StringUtils.isNotBlank(filter.getUserName())) {
            query.append(" and tb.username like :username");
            query.setParam("username", StringUtils.getLikewords(filter.getUserName()));
        }

        if (StringUtils.isNotBlank(filter.getRoleId())) {
            query.append(" and tb.roleId = :roleId");
            query.setParam("roleId", StringUtils.getLikewords(filter.getRoleId()));
        }
        if (StringUtils.isNotBlank(filter.getDeptName())) {
            query.append(" and tb.deptId = :deptId");
            query.setParam("deptId", StringUtils.getLikewords(filter.getDeptId()));
        }
        return query.listResult(filter);
    }

    public List<SysLogin> queryLoginNoPage(SysLoginLogFilter filter) {
        GenericQuery query = createQuery("obj");
        if (StringUtils.isNotBlank(filter.getUserName())) {
            query.append(" and obj.username like:username").setParam("username", StringUtils.getLikewords(filter.getUserName()));
        }
        if (StringUtils.isNotBlank(filter.getState())) {
            query.append(" and obj.state =:state").setParam("state", filter.getState());
        }
        if (StringUtils.isNotBlank(filter.getRoleId())) {
            query.append(" and obj.roleId =:roleId").setParam("roleId", filter.getRoleId());
        }
        if (StringUtils.isNotBlank(filter.getDeptName())) {
                query.append(" and obj.deptName =:deptName").setParam("deptName", StringUtils.getLikewords(filter.getDeptName()));
        }
        return query.listResult(filter);
    }

    public List<SysLogin> queryLowerByRoleDept(String deptId, String roleId) {
        GenericQuery query = createQuery("obj");
        if(StringUtils.isNotBlank(roleId)){
            query.append(" and obj.roleId >:roleId").setParam("roleId", roleId);
            query.append(" and obj.roleId <:role").setParam("role", "10");
        }
        if(StringUtils.isNotBlank(deptId)) {
            query.append(" and obj.deptId =:deptId").setParam("deptId", deptId);
        }
        return query.listResult();
    }

    public List<SysLogin> queryLoginByRoomId(String roomId) {
        GenericQuery query = createQuery("obj");
        if(StringUtils.isNotBlank(roomId)){
            query.append(" and obj.roomId =:roomId").setParam("roomId", roomId);
        }
        return query.listResult();
    }


    public List<SysLogin> queryCountyleader() {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.roleId in (:roleIds)").setParamList("roleIds", new String[]{"10","01"});
        return query.listResult();
    }

    public List<SysLogin> getByDeptId(String deptId) {
        GenericQuery sql = createQuery("sql");
        sql.append(" and state =:state ").setParam("state", "启用");
        sql.append(" and roleId !=:roleId ").setParam("state", "启用");
        sql.append(" and deptId =:deptId").setParam("deptId", deptId);
        return sql.listResult();
    }

    //**
    public List<SysLogin> getByRooId(String roomId) {
        GenericQuery sql = createQuery("sql");
        sql.append(" and roomId =:roomId").setParam("roomId", roomId);
        return sql.listResult();
    }





}
